clear all

********************************************************************************
********************************************************************************
**** When Can We Trust Regression Discontinuity Design Estimates from Close ****
*************** Elections? Evidence from Experimental Benchmarks ***************
********************************************************************************
********************************************************************************

* This file produces Figure OA7 in Online Appendix D of De Magalhaes et al. 
* (Political Analysis).

cd "$dir"
use "$dir/data/personal_incumbency_advantage.dta", clear

********************************************************************************

preserve
DCdensity rv if country=="Brazil" & rv!=0 & abs(rv)<=1, generate(Xj Yj r0 fhat se_fhat) breakpoint(0)
local breakpoint 0
local cellmpname Xj
local cellvalname Yj
local evalname r0
local cellsmname fhat
local cellsmsename se_fhat
tempvar hi
quietly gen `hi' = `cellsmname' + 1.96*`cellsmsename'
tempvar lo
quietly gen `lo' = `cellsmname' - 1.96*`cellsmsename'
drop if `evalname' < -1 | `evalname' > 1
gr twoway  (line `cellsmname' `evalname' if `evalname' < `breakpoint', subtitle("Panel A: Brazil", position(11)) lcolor(black) lwidth(medthick)) ///  
(line `hi' `evalname' if `evalname' < `breakpoint', lcolor(black) lwidth(vthin) lpattern(dash))              ///
(line `lo' `evalname' if `evalname' < `breakpoint', lcolor(black) lwidth(vthin) lpattern(dash)),          ///
name(lines, replace) legend(off) graphregion(color(white))
drop if `cellmpname' < -1 | `cellmpname' > 1
addplot lines: (scatter `cellvalname' `cellmpname', msymbol(circle) mcolor(gs8))             ///
(line `cellsmname' `evalname' if `evalname' > `breakpoint', lpattern(solid) lcolor(black) lwidth(medthick))   ///
(line `hi' `evalname' if `evalname' > `breakpoint', lcolor(black) lwidth(vthin) lpattern(dash))              ///
(line `lo' `evalname' if `evalname' > `breakpoint', lcolor(black) lwidth(vthin) lpattern(dash))  ///
(line `cellsmname' `evalname' if `evalname' < `breakpoint', lpattern(solid) lcolor(black) lwidth(medthick))  ///  
(line `hi' `evalname' if `evalname' < `breakpoint', lcolor(black) lwidth(vthin) lpattern(dash))              ///
(line `lo' `evalname' if `evalname' < `breakpoint', lcolor(black) lwidth(vthin) lpattern(dash)),           ///
xline(`breakpoint', lcolor(black)) legend(off) ylabel(#4,format(%9.2f)) ytitle(Density) xtitle(Running variable) plotregion(lcolor(black)) ///
xlabel(-1(0.5)1, format(%9.2f))
drop Xj Yj r0 fhat se_fhat
graph save "$dir/temp/brazil_density.gph", replace
restore

preserve
DCdensity rv if country=="Colombia" & rv!=0 & abs(rv)<=1, generate(Xj Yj r0 fhat se_fhat) breakpoint(0)
local breakpoint 0
local cellmpname Xj
local cellvalname Yj
local evalname r0
local cellsmname fhat
local cellsmsename se_fhat
tempvar hi
quietly gen `hi' = `cellsmname' + 1.96*`cellsmsename'
tempvar lo
quietly gen `lo' = `cellsmname' - 1.96*`cellsmsename'
drop if `evalname' < -1 | `evalname' > 1
gr twoway  (line `cellsmname' `evalname' if `evalname' < `breakpoint', subtitle("Panel B: Colombia", position(11)) lcolor(black) lwidth(medthick)) ///  
(line `hi' `evalname' if `evalname' < `breakpoint', lcolor(black) lwidth(vthin) lpattern(dash))              ///
(line `lo' `evalname' if `evalname' < `breakpoint', lcolor(black) lwidth(vthin) lpattern(dash)),          ///
name(lines, replace) legend(off) graphregion(color(white))
drop if `cellmpname' < -1 | `cellmpname' > 1
addplot lines: (scatter `cellvalname' `cellmpname', msymbol(circle) mcolor(gs8))             ///
(line `cellsmname' `evalname' if `evalname' > `breakpoint', lpattern(solid) lcolor(black) lwidth(medthick))   ///
(line `hi' `evalname' if `evalname' > `breakpoint', lcolor(black) lwidth(vthin) lpattern(dash))              ///
(line `lo' `evalname' if `evalname' > `breakpoint', lcolor(black) lwidth(vthin) lpattern(dash))  ///
(line `cellsmname' `evalname' if `evalname' < `breakpoint', lpattern(solid) lcolor(black) lwidth(medthick))  ///  
(line `hi' `evalname' if `evalname' < `breakpoint', lcolor(black) lwidth(vthin) lpattern(dash))              ///
(line `lo' `evalname' if `evalname' < `breakpoint', lcolor(black) lwidth(vthin) lpattern(dash)),           ///
xline(`breakpoint', lcolor(black)) legend(off) ylabel(#4,format(%9.2f)) ytitle(Density) xtitle(Running variable) plotregion(lcolor(black)) ///
xlabel(-1(0.5)1, format(%9.2f))
drop Xj Yj r0 fhat se_fhat
graph save "$dir/temp/colombia_density.gph", replace
restore

preserve
DCdensity rv if country=="Denmark" & rv!=0 & abs(rv)<=1, generate(Xj Yj r0 fhat se_fhat) breakpoint(0)
local breakpoint 0
local cellmpname Xj
local cellvalname Yj
local evalname r0
local cellsmname fhat
local cellsmsename se_fhat
tempvar hi
quietly gen `hi' = `cellsmname' + 1.96*`cellsmsename'
tempvar lo
quietly gen `lo' = `cellsmname' - 1.96*`cellsmsename'
drop if `evalname' < -1 | `evalname' > 1
gr twoway  (line `cellsmname' `evalname' if `evalname' < `breakpoint', subtitle("Panel C: Denmark", position(11)) lcolor(black) lwidth(medthick)) ///  
(line `hi' `evalname' if `evalname' < `breakpoint', lcolor(black) lwidth(vthin) lpattern(dash))              ///
(line `lo' `evalname' if `evalname' < `breakpoint', lcolor(black) lwidth(vthin) lpattern(dash)),          ///
name(lines, replace) legend(off) graphregion(color(white))
drop if `cellmpname' < -1 | `cellmpname' > 1
addplot lines: (scatter `cellvalname' `cellmpname', msymbol(circle) mcolor(gs8))             ///
(line `cellsmname' `evalname' if `evalname' > `breakpoint', lpattern(solid) lcolor(black) lwidth(medthick))   ///
(line `hi' `evalname' if `evalname' > `breakpoint', lcolor(black) lwidth(vthin) lpattern(dash))              ///
(line `lo' `evalname' if `evalname' > `breakpoint', lcolor(black) lwidth(vthin) lpattern(dash))  ///
(line `cellsmname' `evalname' if `evalname' < `breakpoint', lpattern(solid) lcolor(black) lwidth(medthick))  ///  
(line `hi' `evalname' if `evalname' < `breakpoint', lcolor(black) lwidth(vthin) lpattern(dash))              ///
(line `lo' `evalname' if `evalname' < `breakpoint', lcolor(black) lwidth(vthin) lpattern(dash)),           ///
xline(`breakpoint', lcolor(black)) legend(off) ylabel(#4,format(%9.2f)) ytitle(Density) xtitle(Running variable) plotregion(lcolor(black)) ///
xlabel(-1(0.5)1, format(%9.2f))
drop Xj Yj r0 fhat se_fhat
graph save "$dir/temp/denmark_density.gph", replace
restore

preserve
DCdensity rv if country=="Finland" & rv!=0 & abs(rv)<=1, generate(Xj Yj r0 fhat se_fhat) breakpoint(0)
local breakpoint 0
local cellmpname Xj
local cellvalname Yj
local evalname r0
local cellsmname fhat
local cellsmsename se_fhat
tempvar hi
quietly gen `hi' = `cellsmname' + 1.96*`cellsmsename'
tempvar lo
quietly gen `lo' = `cellsmname' - 1.96*`cellsmsename'
drop if `evalname' < -1 | `evalname' > 1
gr twoway  (line `cellsmname' `evalname' if `evalname' < `breakpoint', subtitle("Panel D: Finland", position(11)) lcolor(black) lwidth(medthick)) ///  
(line `hi' `evalname' if `evalname' < `breakpoint', lcolor(black) lwidth(vthin) lpattern(dash))              ///
(line `lo' `evalname' if `evalname' < `breakpoint', lcolor(black) lwidth(vthin) lpattern(dash)),          ///
name(lines, replace) legend(off) graphregion(color(white))
drop if `cellmpname' < -1 | `cellmpname' > 1
addplot lines: (scatter `cellvalname' `cellmpname', msymbol(circle) mcolor(gs8))             ///
(line `cellsmname' `evalname' if `evalname' > `breakpoint', lpattern(solid) lcolor(black) lwidth(medthick))   ///
(line `hi' `evalname' if `evalname' > `breakpoint', lcolor(black) lwidth(vthin) lpattern(dash))              ///
(line `lo' `evalname' if `evalname' > `breakpoint', lcolor(black) lwidth(vthin) lpattern(dash))  ///
(line `cellsmname' `evalname' if `evalname' < `breakpoint', lpattern(solid) lcolor(black) lwidth(medthick))  ///  
(line `hi' `evalname' if `evalname' < `breakpoint', lcolor(black) lwidth(vthin) lpattern(dash))              ///
(line `lo' `evalname' if `evalname' < `breakpoint', lcolor(black) lwidth(vthin) lpattern(dash)),           ///
xline(`breakpoint', lcolor(black)) legend(off) ylabel(#4,format(%9.2f)) ytitle(Density) xtitle(Running variable) plotregion(lcolor(black)) ///
xlabel(-1(0.5)1, format(%9.2f))
drop Xj Yj r0 fhat se_fhat
graph save "$dir/temp/finland_density.gph", replace
restore

graph combine "$dir/temp/brazil_density.gph" "$dir/temp/colombia_density.gph" ///
	"$dir/temp/denmark_density.gph" "$dir/temp/finland_density.gph", ///
	cols(2) graphregion(color(white)) xsize(16) ysize(9)
graph export "$dir/figures/figure_OA7.pdf", as(pdf) replace
